﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace SumiaAccountBook.SubControls
{
    /// <summary>
    /// ucConfig.xaml 的交互逻辑
    /// </summary>
    public partial class ucConfig : UserControl
    {
        const int PageSize = 20;
        private int page_count = 1;

        public List<Biz.KeyValueObj> KeyValues { set; get; }

        public ucConfig()
        {
            InitializeComponent();

            bindData();
            this.cb_type.SelectedIndex = 0;
            this.DataContext = this;
        }        

        #region 收入类型

        private void bindData()
        {
            this.cb_type.ItemsSource = Biz.KVConstHelper.GetInstance().GetAllValue<Biz.KVCashFlowType>();            

            int t1 = 0;
            this.KeyValues = Biz.GetList.GetListKeyValue(this.page_count, PageSize, ref t1);
            this.dg.ItemsSource = this.KeyValues;
            this.dp.TotalCount = t1;
        }

        private void dp_PageChanged(object sender, CubicFamily.StockTool.ComonControl.PageChangedEventArgs e)
        {
            this.page_count = e.CurrentPageIndex;
            bindData();           
        }

        private void dg_remove_Click(object sender, RoutedEventArgs e)
        {            
            var original = e.OriginalSource as Button;
            var key = original.CommandParameter;
            int Id = Convert.ToInt32(key);
            MessageBoxResult confirmToDel = MessageBox.Show("确认要删除所选行吗？", "提示", MessageBoxButton.YesNo, MessageBoxImage.Question);
            if (confirmToDel == MessageBoxResult.Yes)
            {
                try
                {
                    DataAccess.DaCommon<DataAccess.Entity.KeyValue>.Delete(Id);
                }catch(Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                
                bindData();
            }
        }

        private void btn_add_Click(object sender, RoutedEventArgs e)
        {
            string tmpType = this.cb_type.SelectedValue as string;
            if (string.IsNullOrEmpty(tmpType))
            {
                MessageBox.Show("请选择类型！");                
            }
            int type = Biz.KVConstHelper.GetInstance().GetKey<Biz.KVCashFlowType>(tmpType);
            string value = this.tb_name.Text.Trim();
            if(string.IsNullOrEmpty(value))
            {
                MessageBox.Show("请填写名称！");
            }
            string memo = this.tb_memo.Text.Trim();
            try
            {
                DataAccess.DaCommon<DataAccess.Entity.KeyValue>.Add(
                    new DataAccess.Entity.KeyValue { Type = type, Value = value, Memo = memo });
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            
            bindData();
        }

        #endregion


    }
}
